﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.IO;
using System.Collections.ObjectModel;
namespace _3DHouse
{
    /// <summary>
    /// OpenFile.xaml 的交互逻辑
    /// </summary>
    public partial class OpenFile : Window
    {
        public OpenFile()
        {
            InitializeComponent();
           dgHouseFile.DataContext= GetHouseFile();
            List<string> qianitems = new List<string>();
            List<string> fangitems = new List<string>();
            for (int i = 1; i <= 100; i++)
            {
                string num = i.ToString();
                num = num.PadLeft(13, '0');
                qianitems.Add(num);
                num = i.ToString().PadLeft(4, '0');
                fangitems.Add(num);
            }
            comboBox3.ItemsSource = fangitems;
            comboBox1.ItemsSource = qianitems;
            comboBox1.SelectedIndex = 0;
            comboBox3.SelectedIndex = 0;
        dataGrid1. DataContext=   GetData();
        
            //下载文件
          streamHelper = new FileManager.Client.FileStreamService.StreamHelper("1", "1");
      try
            {  
          streamHelper.DownLoadFileCompleted += (a, b) =>
        {
            if (b.IsSuccess)
            {
                MemoryStream ms = new MemoryStream(b.Datas.ToArray());              
                this.Dispatcher.Invoke(new Action(() =>
                    {
                        
                        //打开新窗口
                        Drawing xin = new Drawing(ms,houseNumber);                       
                        xin.Show();                       
                    }));
            }
            else
            {
                MessageBox.Show("下载失败，"+b.ErrorMessage);
            }

        };
            }
      catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }        
        FileManager.Client.FileStreamService.StreamHelper streamHelper;
       public FileStream fileStream;
        /// <summary>
        /// 户型图编号
        /// </summary>
       public string houseNumber = "";//不包含后缀
       string path = Directory.GetCurrentDirectory() + "\\house";
        //打开服务器文件
        private void button1_Click(object sender, RoutedEventArgs e)
        {    
            try
            {
                //调用下载方法
                streamHelper.DownLoadFile(houseNumber+".xml", FileManager.Client.FileStreamService.FileManagerService.FileUsageType.Image);

             this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }
        }

        private void dataGrid1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {              
            HouseMap row = dataGrid1.SelectedValue as HouseMap;
            houseNumber = row.HmID;
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
        //获得服务器信息
        private ObservableCollection<HouseMap> GetData()
        {
            ObservableCollection<HouseMap> list = new ObservableCollection<HouseMap>();            
            HouseMap house = new HouseMap();
            house.DaId = "1";
            house.HmDate = "2014-3-6 15:19:12";
            house.HmID = "0000000000001_0001_201403081646";
            house.HmMark = "备注";
            house.HmVersion = "2014361519";
            house.StCode = "0000000000001";
            house.UserId = "122212";
            house.State = "未通过";
            list.Add(house);
            HouseMap house2 = new HouseMap();
            house2.DaId = "2";
            house2.HmDate = "2014-3-6 15:19:12";
            house2.HmID = "0000000000001_0001_201403081512";
            house2.HmMark = "备注";
            house2.HmVersion = "2014361519";
            house2.StCode = "0000000000002";
            house2.UserId = "122212";
            house2.State = "未审核";
            list.Add(house2);
            HouseMap house3 = new HouseMap();
            house3.DaId = "3";
            house3.HmDate = "2014-3-6 15:19:12";
            house3.HmID = "0000000000001_0001_201403101330";
            house3.HmMark = "备注";
            house3.HmVersion = "2014361519";
            house3.StCode = "0000000000003";
            house3.UserId = "122212";
            house3.State = "未审核";
            list.Add(house3);
            return list;
        }
        //获得本地未上传文件
        ObservableCollection<HouseMap> GetHouseFile()
        {
           
            if (!Directory.Exists(path))
            {
                MessageBox.Show("tools不存在");
                return null;
            }
 ObservableCollection<HouseMap> list = new ObservableCollection<HouseMap>();
            DirectoryInfo dir = new DirectoryInfo(path);
            FileInfo[] chiFiles = dir.GetFiles("*.xml");
            //遍历本地未上传文件
            foreach (FileInfo fl in chiFiles)
            {          
                //截取防区编号，从数据库获取防区信息
                string fnum = fl.Name.Substring(fl.Name.IndexOf("_")+1,4);//防区编号
                HouseMap house = new HouseMap();
                house.DaId = fnum;
                house.HmDate = "2014-3-6 15:19:12";
                house.HmID = fl.Name.Substring(0, fl.Name.IndexOf("."));
                house.HmMark = "备注";
                house.HmVersion = "2014361519";
                house.StCode = "0000000000001";
                house.UserId = "122212";
               
                list.Add(house);
            }
            return list;
        }
     
        class HouseMap
        {
            public string HmID { get; set; }
            public string StCode { get; set; }
            public string DaId { get; set; }
            public string HmDate { get; set; }
            public string UserId { get; set; }
            public string HmVersion { get; set; }
            public string State { get; set; }
            public string HmMark { get; set; }
        }
       //打开本地文件
        private void button3_Click(object sender, RoutedEventArgs e)
        {
            string m_filePath = path + "\\" + houseNumber + ".xml" ;
            Drawing xin = new Drawing(houseNumber,m_filePath);         
            xin.Show();
            this.Close();
        }

        private void dgHouseFile_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            HouseMap row = dgHouseFile.SelectedValue as HouseMap;
            houseNumber = row.HmID;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            houseNumber = ((Button)sender).Tag.ToString();
            Drawing xin = new Drawing();
            xin.houseNum = houseNumber.Substring(0, houseNumber.LastIndexOf("_")) +"_"+ DateTime.Now.ToString("yyyyMMddHHmm");
            xin.Show();
            this.Close();
        }

        private void button4_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

       
    }
}
